package com.dqy.twserver.server.support;

import com.dqy.twserver.server.dto.GameDto;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class MsgPush {
    static Map<String,Field> fieldCach=new ConcurrentHashMap<>(); //缓存提高反射性能
    public static void sendMsg(List<Player> list,String msgInfo,String fiedName,int eveType) throws NoSuchFieldException, IllegalAccessException {

        Field declaredField=fieldCach.get(fiedName);
        if(declaredField==null){
            declaredField = GameDto.class.getDeclaredField(fiedName);
            declaredField.setAccessible(true);
            fieldCach.put(fiedName,declaredField);
        }

        for(Player player : list){
            GameDto msg=new GameDto();
            msg.setEventType(eveType);
            msg.setPlayerName(player.playerName);
            declaredField.set(msg,msgInfo);
            if(player.getMutilSocktChannel()!=null){
                player.getMutilSocktChannel().writeAndFlush(msg);
            }
        }
    }
}
